package com.liangdemo.web.exception;

import com.liangdemo.web.exception.util.ResourceUtil;
import com.liangdemo.web.utils.ErrorCode;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * 异常处理类
 *
 * @author: LiangErLe
 * @date: 2022/8/10
 **/
@ControllerAdvice
public class ResponseExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(value = DemoException.class)
    protected ResponseEntity<Object> DemoException(RuntimeException e, WebRequest webRequest) {
        String msg = ResourceUtil.getErrorMessage(e.getMessage() == null ? ErrorCode.TEST : e.getMessage(), null);
        return handleExceptionInternal(e, msg, new HttpHeaders(), HttpStatus.OK, webRequest);
    }
}
